package edu.lsnu.equipment_management_system.controller;

import edu.lsnu.equipment_management_system.mapper.select.nopaging.AdminOptionMapper;
import edu.lsnu.equipment_management_system.pojo.domain.JsonData;
import edu.lsnu.equipment_management_system.pojo.domain.PageBean;
import edu.lsnu.equipment_management_system.pojo.domain.UseStatusConstant;
import edu.lsnu.equipment_management_system.pojo.vo.out.admin.*;
import edu.lsnu.equipment_management_system.pojo.vo.sql_conditions.DateAndEquipmentNoVo;
import edu.lsnu.equipment_management_system.service.AdminOptionService;
import edu.lsnu.equipment_management_system.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * @author mzy
 * @Description 管理员的界面内容
 * @date 2018/11/16
 */
@RestController
public class AdminOptionController {
    @Autowired
    AdminOptionService adminOptionService;

    @Autowired
    AdminOptionMapper mapper;

    // -----------管理员的页面数据展示-----------
    /**
     * @Description 购买设备的记录列表
     * @author mzy
     * @time 2018-11-19
     * @see
     * @return   java.lang.Object
     * @params  []
     */
    @GetMapping("/api/admin/purchaseList/{pageNum}")
    public Object getPurchaseList(@PathVariable(value = "pageNum") Integer pageNum) {
        PageBean<AdminMsgInVo> equipmentPurchaseBasicListVo =
                adminOptionService.getEquipmentPurchaseBasicListVo(pageNum);

        return JsonData.buildSuccess(0, equipmentPurchaseBasicListVo,
                "操作成功");

    }

    /**
     * @Description 购买设备的记录详情
     * @author mzy
     * @time 2018-11-19
     * @see
     * @return   java.lang.Object
     * @params  []
     */
    @GetMapping("/api/admin/purchaseDetail/{equipmentNo}")
    public Object getPurchaseDetail(@PathVariable("equipmentNo") String equipmentNo) {
        equipmentNo = equipmentNo.trim();
        AdminEquipmentPurchaseVo equipmentPurchaseDetailVo =
                mapper.getEquipmentPurchaseDetailVo(equipmentNo);

        if (null == equipmentPurchaseDetailVo) {
            return JsonData.buildError("操作失败");
        } else {
            return JsonData.buildSuccess(0, equipmentPurchaseDetailVo,
                    "操作成功");
        }

    }
    // ------------------------------------------------
    /**
     * @Description 调入设备的记录列表
     * @author mzy
     * @time 2018-11-19
     * @see
     * @return   java.lang.Object
     * @params  []
     */
    @GetMapping("/api/admin/transferInList/{pageNum}")
    public Object getTransferInList(@PathVariable(value = "pageNum") Integer pageNum) {
        PageBean<AdminMsgInVo> equipmentTransferInBasicListVo =
                adminOptionService.getEquipmentTransferInBasicListVo(pageNum);

        if (null == equipmentTransferInBasicListVo) {
            return JsonData.buildError("操作失败");
        } else {
            return JsonData.buildSuccess(0, equipmentTransferInBasicListVo,
                    "操作成功");
        }
    }

    /**
     * @Description 调入设备的记录详情
     * @author mzy
     * @time 2018-11-19
     * @see
     * @return   java.lang.Object
     * @params  []
     */
    @GetMapping("/api/admin/transferInDetail/{equipmentNo}")
    public Object getTransferInDetail(@PathVariable("equipmentNo") String equipmentNo) {
        equipmentNo = equipmentNo.trim();
        AdminEquipmentTransferInVo equipmentTransferInDetailVo =
                mapper.getEquipmentTransferInDetailVo(equipmentNo);

        if (null == equipmentTransferInDetailVo) {
            return JsonData.buildError("操作失败");
        } else {
            return JsonData.buildSuccess(0, equipmentTransferInDetailVo,
                    "操作成功");
        }
    }
    // ------------------------------------------------
    /**
     * @Description 维修设备的记录列表
     * @author mzy
     * @time 2018-11-19
     * @see
     * @return   java.lang.Object
     * @params  []
     */
    @GetMapping("/api/admin/repairList/{pageNum}")
    public Object getRepairList(@PathVariable(value = "pageNum") Integer pageNum) {
        PageBean<AdminMsgInRepairVo> equipmentRepairBasicListVo =
                adminOptionService.getEquipmentRepairBasicListVo(pageNum);

        if (null == equipmentRepairBasicListVo) {
            return JsonData.buildError("操作失败");
        } else {
            return JsonData.buildSuccess(0, equipmentRepairBasicListVo,
                    "操作成功");
        }
    }

    /**
     * @Description 维修设备的记录详情
     * @author mzy
     * @time 2018-11-19
     * @see
     * @return   java.lang.Object
     * @params  []
     */
    @GetMapping("/api/admin/repairDetail/{equipmentNo}/{repairDate}")
    public Object getRepairDetail(@PathVariable String equipmentNo, @PathVariable String repairDate) {
        equipmentNo = equipmentNo.trim();
        Date date = DateUtil.parseDate(repairDate);

        DateAndEquipmentNoVo dateAndEquipmentNoVo = new DateAndEquipmentNoVo();
        dateAndEquipmentNoVo.setDate(date);
        dateAndEquipmentNoVo.setEquipmentNo(equipmentNo);

        AdminEquipmentRepairVo equipmentRepairDetailVo =
                mapper.getEquipmentRepairDetailVo(dateAndEquipmentNoVo);

        if (null == equipmentRepairDetailVo) {
            return JsonData.buildError("操作失败");
        } else {
            return JsonData.buildSuccess(0, equipmentRepairDetailVo,
                    "操作成功");
        }
    }
    // ------------------------------------------------
    /**
     * @Description 调出设备的记录列表
     * @author mzy
     * @time 2018-11-19
     * @see
     * @return   java.lang.Object
     * @params  []
     */
    @GetMapping("/api/admin/transferOutList/{pageNum}")
    public Object getTransferOutList(@PathVariable("pageNum") Integer pageNum) {
        PageBean<AdminMsgInVo> equipmentTransferOutBasicListVo =
                adminOptionService.getEquipmentTransferOutBasicListVo(pageNum);

        if (null == equipmentTransferOutBasicListVo) {
            return JsonData.buildError("操作失败");
        } else {
            return JsonData.buildSuccess(0, equipmentTransferOutBasicListVo,
                    "操作成功");
        }
    }

    /**
     * @Description 调出设备的记录详情
     * @author mzy
     * @time 2018-11-19
     * @see
     * @return   java.lang.Object
     * @params  []
     */
    @GetMapping("/api/admin/transferOutDetail/{equipmentNo}")
    public Object getTransferOutDetail(@PathVariable("equipmentNo") String equipmentNo) {
        equipmentNo = equipmentNo.trim();
        AdminEquipmentTransferOutVo equipmentTransferOutDetailVo =
                mapper.getEquipmentTransferOutDetailVo(equipmentNo);

        if (null == equipmentTransferOutDetailVo) {
            return JsonData.buildError("操作失败");
        } else {
            return JsonData.buildSuccess(0, equipmentTransferOutDetailVo,
                    "操作成功");
        }
    }

    // ------------------------------------------------
    /**
     * @Description 报损报废设备的记录列表
     * @author mzy
     * @time 2018-11-19
     * @see
     * @return   java.lang.Object
     * @params  []
     */
    @GetMapping("/api/admin/damageList/{pageNum}")
    public Object getDamageList(@PathVariable("pageNum") Integer pageNum) {
        PageBean<AdminMsgInVo> equipmentDamageBasicListVo =
                adminOptionService.getEquipmentDamageBasicListVo(pageNum);

        if (null == equipmentDamageBasicListVo) {
            return JsonData.buildError("操作失败");
        } else {
            return JsonData.buildSuccess(0, equipmentDamageBasicListVo,
                    "操作成功");
        }
    }

    /**
     * @Description 报损报废设备的记录详情
     * @author mzy
     * @time 2018-11-19
     * @see
     * @return   java.lang.Object
     * @params  []
     */
    @GetMapping("/api/admin/damageDetail/{equipmentNo}")
    public Object getDamageDetail(@PathVariable("equipmentNo") String equipmentNo) {
        equipmentNo = equipmentNo.trim();
        AdminEquipmentDamageVo equipmentDamageDetailVo =
                mapper.getEquipmentDamageDetailVo(equipmentNo);

        if (null == equipmentDamageDetailVo) {
            return JsonData.buildError("操作失败");
        } else {
            return JsonData.buildSuccess(0, equipmentDamageDetailVo,
                    "操作成功");
        }
    }

    // ------------------------------------------------
    /**
     * @Description 运行使用设备的记录列表：注意运行使用没有详情
     * @author mzy
     * @time 2018-11-24
     * @see
     * @return   java.lang.Object
     * @params  []
     */
    @GetMapping("/api/admin/useRecord/{pageNum}")
    public Object getEquipmentUseRecordVo(@PathVariable Integer pageNum) {
        PageBean<AdminUseRecordVo> equipmentUseRecordVo =
                adminOptionService.getEquipmentUseRecordVo(pageNum);

        if (null == equipmentUseRecordVo) {
            return JsonData.buildError("操作失败");
        } else {
            return JsonData.buildSuccess(0, equipmentUseRecordVo,
                    "操作成功");
        }
    }

    // ------------------------------------------------
    /**
     * @Description 获得某个设备的使用记录：通过equipmentNo
     * @author mzy
     * @time 2018-11-24
     * @see
     * @return   java.lang.Object
     * @params  []
     */
    @GetMapping("/api/admin/useRecord/{equipmentNo}/{pageNum}")
    public Object getEquipmentUseRecordVo(@PathVariable String equipmentNo, @PathVariable Integer pageNum) {
        equipmentNo = equipmentNo.trim();
        PageBean<AdminUseRecordVo> equipmentUseRecordVo =
                adminOptionService.getEquipmentUseRecordVoByEquipmentNo(pageNum, equipmentNo);
        System.err.println("equipmentUseRecordVo = " + equipmentUseRecordVo.getContent());
        if (null == equipmentUseRecordVo) {
            return JsonData.buildError("操作失败");
        } else {
            return JsonData.buildSuccess(0, equipmentUseRecordVo,
                    "操作成功");
        }
    }
}
